package cn.zcx;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.07.19.45
 */
public class T18 {
    Lock lock = new ReentrantLock();
    void m1(){
        lock.lock();
        System.out.println("线程1启动");
        try{
            TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
    void m2(){
        try {
            lock.lockInterruptibly();
            System.out.println("线程2终于打断了");
        } catch (InterruptedException e) {
            System.out.println("编程2....");
        }finally {
            if(lock.tryLock())
                lock.unlock();
            System.out.println("线程2结束了");
            System.exit(0);
        }
    }
    public static void main(String[] args) {

        var t = new T18();
        new Thread(t::m1,"T1").start();
        Thread t2 = new Thread(t::m2,"T2");
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
        t2.interrupt();
        System.out.println(Thread.activeCount());
    }
}
